package com.spark.sparkanswerserver.dao;

import com.spark.sparkanswerserver.domain.Node;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.repository.query.Query;

import java.util.List;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2023/2/22 14:48
 */
@EnableNeo4jRepositories
public interface NodeRepository extends Neo4jRepository<Node,Long> {
    @Query("match(n:health) where n.name = $name return n")
    Node findAllById(String name);

    /**
     * 创建节点
     * @param name
     * @return
     */
    @Query("create(n:health{name:$name}) return n")
    Node CreateNode(String name);

    /**
     * 创建关系
     * @param name1
     * @param name2
     */
    @Query("match (n:health),(m:health) where n.name=$name1 and m.name=$name2 create (n)-[r:`引发`]->(m)")
    public void CreateRelation(String name1,String name2);

    /**
     * 删除结果关系
     * @param name
     */
    @Query("match (n)-[r:主治]->(m) where n.name=$name delete r")
    public void DeleteResultRelation(String name);

    /**
     * 删除原因关系
     * @param name
     */
    @Query("match (n)-[r:引发]->(m) where m.name=$name delete r")
    public void DeleteReasonRelation(String name);

    /**
     * 删除节点
     * @param name
     */
    @Query("match (n:health) where n.name=$name delete n")
    public void deleteByName(String name);
    @Query("MATCH (n:health)-[r]->(m:health) where n.id=$id RETURN type(r)")
    public List FindAll(Long id);

    @Query("MATCH p=(n)-->() where n.name=$name RETURN p")
    public List<Node> FindALL2(String name);

    /**
     * 查询病因
     * @param name
     * @return
     */
    @Query("MATCH (n:health)-[r:主治]->(m:health) where m.name= $name RETURN n")
    public List<Node> MatchReason(String name);

    /**
     * 查询并发症
     * @param name
     * @return
     */
    @Query("MATCH (n:health)-[r:主治]->(m:health) where n.name= $name RETURN m")
    public List<Node> MatchResult(String name);

    /**
     * 查询关系
     * @param name1
     * @param name2
     * @return
     */
    @Query("MATCH (n:health)-[r]->(m:health) where n.name=$name1 and m.name=$name2 RETURN type(r)")
    public String Type(String name1, String name2);

    /**
     * 获取全部节点
     * @return
     */
    @Query("MATCH p=()-->() RETURN p")
    public List<Node> MatchAll();

    /**
     * 获取元素数目
     * @param name
     * @return
     */
    @Query("Match (n:health) where n.name=$name return count(n)")
    public Integer MatchNumber(String name);

    @Query("MATCH (n:health) RETURN n skip $number limit $page")
    public List<Node> MatchAllNodes(Integer number, Integer page);

    @Query("match (n:health) return count(n)")
    public Integer MatchNumber();
    @Query("MATCH (n:wcros) RETURN n.name as name")
    public List<String> MatchString();
    @Query("MATCH (n:wcros) RETURN n.wid as id")
    public List<Integer> MatchInteger();
    @Query("Match (n:wcros)-[]->(m) where n.name = $name RETURN m.name")
    public List<String> MatchOne(String name);
}